package com.example.bruger.androidneurosky;

import android.app.FragmentManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.Timer;


public class MedAttGameActivity extends ActionBarActivity implements TGCallBack {
    TextView timeTextView;
    int currentMed;
    int currentAtt;
    float completedCycles = 0;
    Timer timer;
    long startTime = 0;
    long totalTime = 0;
    float score = 10000;
    Handler timerHandler = new Handler();
    boolean started = false;
    boolean meditationCycle = true;
    int minMeditation = 75;
    int minAttention = 75;
    long updateBlockTime = 0;
    long updateBlockInterval = 250;

    DbHighScoreHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_med_att_game);
        db = new DbHighScoreHelper(this);

        TGHandler.SetNewCallBack(this);

        Button startBtn = (Button) findViewById(R.id.startBtn);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StartStopGame();
            }
        });
    }

    private void StartStopGame() {
        Button startBtn = (Button) findViewById(R.id.startBtn);
        if (started == false) {
            startBtn.setText("Stop");
            started = true;
            timeTextView = (TextView) findViewById(R.id.totalTimeTextView);

            startTime = System.currentTimeMillis();
            timer = new Timer();
            startTime = System.currentTimeMillis();
            timerHandler.postDelayed(timerRunnable, 0);
        }
       else {
            started = false;
            startBtn.setText("Start");
            timerHandler.removeCallbacks(timerRunnable);
            FragmentManager fragmentManager = getFragmentManager();
            HighScoreDialog dialog = new HighScoreDialog();
            Bundle args = new Bundle();
            dialog.setContext(this);
            args.putFloat("Score", score);
            dialog.setArguments(args);
            dialog.show(fragmentManager, "dialog");
        }
    }

    Runnable timerRunnable = new Runnable() {

        @Override
        public void run() {
            totalTime = System.currentTimeMillis() - startTime;
            int seconds = (int) (totalTime / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            timeTextView.setText(String.format("%02d:%02d", minutes, seconds));

            timerHandler.postDelayed(this, 500);
        }
    };

    @Override
    public void Update(int attention, int meditation, int delta, int theta,
                       int lowAlpha, int highAlpha, int lowBeta, int highBeta,
                       int lowGamma, int midGamma) {
        currentMed = meditation;
        currentAtt = attention;

        if (meditationCycle) {
            ((TextView) findViewById(R.id.medTextView)).setTextColor(Color.GREEN);
            ((TextView) findViewById(R.id.attTextView)).setTextColor(Color.BLACK);
            ((ProgressBar) findViewById(R.id.medProgressBar)).setProgress(currentMed);
            ((TextView) findViewById(R.id.medProgressTextView)).setText(currentMed + "/100");

            // Reset Attention
            ((ProgressBar) findViewById(R.id.attProgressBar)).setProgress(0);
            ((TextView) findViewById(R.id.attProgressTextView)).setText(0 + "/100");
        }
        else {
            ((TextView) findViewById(R.id.attTextView)).setTextColor(Color.GREEN);
            ((TextView) findViewById(R.id.medTextView)).setTextColor(Color.BLACK);
            ((ProgressBar) findViewById(R.id.attProgressBar)).setProgress(currentAtt);
            ((TextView) findViewById(R.id.attProgressTextView)).setText(currentAtt + "/100");

            // Reset Meditation
            ((ProgressBar) findViewById(R.id.medProgressBar)).setProgress(0);
            ((TextView) findViewById(R.id.medProgressTextView)).setText(0 + "/100");
        }

        if (currentMed >= minMeditation && meditationCycle && updateBlockTime <= totalTime) {
            updateBlockTime = totalTime + updateBlockInterval;
            completedCycles += 0.5;
            ((TextView) findViewById(R.id.completedCycTextView)).setText(completedCycles + " cycles");
            meditationCycle = false;
        }
        if (currentAtt >= minAttention && !meditationCycle && updateBlockTime <= totalTime) {
            updateBlockTime = totalTime + updateBlockInterval;
            completedCycles += 0.5;
            ((TextView) findViewById(R.id.completedCycTextView)).setText(completedCycles + " cycles");
            meditationCycle = true;
        }
        float cycleAvg = (float)Math.round(((totalTime / 1000f) / completedCycles * 100)) / 100;
        if (completedCycles >= 1 && totalTime > 10000 && cycleAvg < score && cycleAvg < 100000 && cycleAvg > 0) {
            score = cycleAvg;
            ((TextView) findViewById(R.id.maxScoreTextView)).setText(score + " sec/cycle");
        }
        ((TextView) findViewById(R.id.avgTxtView)).setText(cycleAvg + " sec/cycle");
    }
}
